home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
SGI Developer Toolbox 6.1
/
SGI Developer Toolbox 6.1 - Disc 4.iso
/
public
/
Xprof
/
xmeasure
/
tstgfx.c
< prev
next >
Wrap
C/C++ Source or Header
|
1994-08-01
|
20KB
|
849 lines
/*==================================================================
* File : tstgfx.c
* Package: Xmeasure
*
* Author : Aloke Gupta.
*
* (C) Copyright 1992, Aloke Gupta.
* All rights granted to University of Illinois Board of Regents.
*==================================================================*/
#include "perf.h"
#define POLYOBJECTS 8 /* Number of objects for Poly requests */
#define XINC_LINE 64
#define YINC_LINE 64
#define XINC_RECT 1
#define YINC_RECT 2
#define XOFFSET_RECT 2
#define YOFFSET_RECT 2
/* Shared variables */
static int i;
static XPoint *points;
static XSegment *segments;
static XRectangle *rectangles;
static XArc *arcs;
static XImage *image;
static Pixmap pixmap;
static Pixmap *pixmaps;
static GC mygc;
static GC *GCs;
static XGCValues gcval;
static unsigned long gcmask;
/* ====================================================================== */
int CreatePixmap(xd, xp)
XD *xd;
XParams *xp;
{
int width = 16, height = 16;
for (i = 0; i < xp->itns; i++) {
pixmaps[i] = XCreatePixmap(xd->display, xd->window, width, height,
DefaultDepth(xd->display, xd->screen ));
if (pixmaps[i] == NULL) {
/*
fprintf(stderr, "CreatePixmap: Can't allocate %dth pixmap", i);
*/
xp->itns = i;
break;
}
}
}
int i_CreatePixmap(xd, xp)
XD *xd;
XParams *xp;
{
pixmaps = (Pixmap *) malloc (xp->itns * sizeof(Pixmap));
}
int c_CreatePixmap(xd, xp)
XD *xd;
XParams *xp;
{
for (i = 0; i < xp->itns; i++)
XFreePixmap(xd->display, pixmaps[i]);
free(pixmaps);
}
/* ====================================================================== */
int FreePixmap(xd, xp)
XD *xd;
XParams *xp;
{
for (i = 0; i < xp->itns; i++)
XFreePixmap(xd->display, pixmaps[i]);
}
int i_FreePixmap(xd, xp)
XD *xd;
XParams *xp;
{
int width = 8, height = 8;
pixmaps = (Pixmap *) malloc (xp->itns * sizeof(Pixmap));
for (i = 0; i < xp->itns; i++) {
pixmaps[i] = XCreatePixmap(xd->display, xd->window, width, height,
DefaultDepth(xd->display, xd->screen ));
if (pixmaps[i] == NULL) {
/*
fprintf(stderr, "Can't allocate %dth pixmap", i);
*/
xp->itns = i;
break;
}
}
}
int c_FreePixmap(xd, xp)
XD *xd;
XParams *xp;
{
free(pixmaps);
}
/* ====================================================================== */
int CreateGC(xd, xp)
XD *xd;
XParams *xp;
{
for (i = 0; i < xp->itns; i++)
GCs[i] = XCreateGC(xd->display, xd->window, gcmask, &gcval);
}
int i_CreateGC(xd, xp)
XD *xd;
XParams *xp;
{
GCs = (GC *) malloc(xp->itns * sizeof(GC));
gcmask = GCFunction | GCLineWidth | GCLineStyle | GCFillStyle;
gcval.function = GXxor;
gcval.line_width = 1;
gcval.line_style = LineOnOffDash;
gcval.fill_style = FillStippled;
}
int c_CreateGC(xd, xp)
XD *xd;
XParams *xp;
{
for (i = 0; i < xp->itns; i++)
XFreeGC(xd->display, GCs[i]);
free(GCs);
}
/* ====================================================================== */
/* Note: XChangeGC measurement is meaningless since Xlib has a cache for GC
attributes.
*/
int ChangeGC(xd, xp)
XD *xd;
XParams *xp;
{
for (i = 0; i < xp->itns; i++) {
if (i % 2) gcval.function = GXcopy;
else gcval.function = GXxor;
XChangeGC(xd->display, mygc, gcmask, &gcval);
}
}
int i_ChangeGC(xd, xp)
XD *xd;
XParams *xp;
{
mygc = XCreateGC(xd->display, xd->window, 0, 0);
gcmask = GCFunction | GCLineWidth | GCLineStyle | GCFillStyle |
GCForeground | GCBackground;
gcval.function = GXxor;
gcval.line_width = 1;
gcval.line_style = LineOnOffDash;
gcval.fill_style = FillStippled;
gcval.foreground = WhitePixel(xd->display, xd->screen);
gcval.background = BlackPixel(xd->display, xd->screen);
}
int c_ChangeGC(xd, xp)
XD *xd;
XParams *xp;
{
XFreeGC(xd->display, mygc);
}
/* ====================================================================== */
int CopyGC(xd, xp)
XD *xd;
XParams *xp;
{
for (i = 0; i < xp->itns; i++)
XCopyGC(xd->display, GCs[0], gcmask, GCs[1]);
}
int i_CopyGC(xd, xp)
XD *xd;
XParams *xp;
{
GCs = (GC *) malloc(2 * sizeof(GC));
GCs[0] = XCreateGC(xd->display, xd->window, 0, 0);
GCs[1] = XCreateGC(xd->display, xd->window, 0, 0);
gcmask = ~0; /* Worst case: Copy all the attributes */
gcmask = GCFunction | GCLineWidth | GCLineStyle | GCFillStyle |
GCForeground | GCBackground;
}
int c_CopyGC(xd, xp)
XD *xd;
XParams *xp;
{
XFreeGC(xd->display, GCs[0]);
XFreeGC(xd->display, GCs[1]);
free(GCs);
}
/* ====================================================================== */
int SetDashes(xd, xp)
XD *xd;
XParams *xp;
{
static char dash2[] = {6, 3};
static char dash4[] = {9, 3, 3, 3};
for (i = 0; i < xp->itns; i++) {
if (i % 2) XSetDashes(xd->display, mygc, 0, dash2, 2);
else XSetDashes(xd->display, mygc, 0, dash4, 4);
}
}
int i_SetDashes(xd, xp)
XD *xd;
XParams *xp;
{ mygc = XCreateGC(xd->display, xd->window, 0, 0); }
int c_SetDashes(xd, xp)
XD *xd;
XParams *xp;
{ XFreeGC(xd->display, mygc); }
/* ====================================================================== */
static XRectangle crect4[4];
int SetClipRectangles(xd, xp)
XD *xd;
XParams *xp;
{
for (i = 0; i < xp->itns; i++) {
if (i % 2)
XSetClipRectangles(xd->display, mygc, 0, 0, crect4, 2, Unsorted);
else XSetClipRectangles(xd->display, mygc, 0, 0, crect4, 4, Unsorted);
}
}
int i_SetClipRectangles(xd, xp)
XD *xd;
XParams *xp;
{
mygc = XCreateGC(xd->display, xd->window, 0, 0);
for (i = 0; i < 4; i++) {
crect4[i].x = DISPWIDTH / (i + 1);
crect4[i].y = DISPHEIGHT / (i + 1);
crect4[i].width = DISPWIDTH / (i + 1);
crect4[i].height = DISPHEIGHT / (i + 1);
}
}
int c_SetClipRectangles(xd, xp)
XD *xd;
XParams *xp;
{ XFreeGC(xd->display, mygc); }
/* ====================================================================== */
int FreeGC(xd, xp)
XD *xd;
XParams *xp;
{
for (i = 0; i < xp->itns; i++)
XFreeGC(xd->display, GCs[i]);
}
int i_FreeGC(xd, xp)
XD *xd;
XParams *xp;
{
GCs = (GC *) malloc(xp->itns * sizeof(GC));
for (i = 0; i < xp->itns; i++)
GCs[i] = XCreateGC(xd->display, xd->window, 0, 0);
}
int c_FreeGC(xd, xp)
XD *xd;
XParams *xp;
{
free(GCs);
}
/* ====================================================================== */
/* ClearArea: Sizepair => (width, height) */
/* size => area = width * height */
int ClearArea(xd, xp)
XD *xd;
XParams *xp;
{
int width, height;
width = xp->sizepair->s1;
height = xp->sizepair->s2;
xp->size = width * height;
for (i = 0; i < xp->itns; i++)
XClearArea(xd->display, xd->window, 0, 0, width, height, True);
}
int i_ClearArea(xd, xp)
XD *xd;
XParams *xp;
{
XFillRectangle(xd->display, xd->window, xd->gc, 0, 0,
DISPWIDTH, DISPHEIGHT);
xp->size = xp->sizepair->s1 * xp->sizepair->s2;
}
int c_ClearArea(xd, xp)
XD *xd;
XParams *xp;
{
}
/* ====================================================================== */
/* CopyArea: Sizepair => (width, height) */
/* size => area = width * height */
/* The code here only measures the time taken to copy from pixmap to window */
int CopyArea(xd, xp)
XD *xd;
XParams *xp;
{
int width, height;
int dst_x = 0, dst_y = 0;
width = xp->sizepair->s1;
height = xp->sizepair->s2;
for (i = 0; i < xp->itns; i++) {
XCopyArea(xd->display, pixmap, xd->window, xd->gc, 0, 0,
width, height, dst_x, dst_y);
if (height < DISPHEIGHT / 2)
dst_y += height;
else dst_y ++;
if ((dst_y + height) > DISPHEIGHT) {
dst_y %= DISPHEIGHT;
dst_x ++;
if ((dst_x + width ) > DISPWIDTH )
dst_x %= DISPWIDTH;
}
}
}
int i_CopyArea(xd, xp)
XD *xd;
XParams *xp;
{
int width, height;
width = xp->sizepair->s1;
height = xp->sizepair->s2;
xp->size = width * height;
RandomFill(xd, xp);
pixmap = XCreatePixmap(xd->display, xd->window, width, height,
DefaultDepth(xd->display, xd->screen ));
XCopyArea(xd->display, xd->window, pixmap, xd->gc, 0, 0,
width, height, 0, 0);
}
int c_CopyArea(xd, xp)
XD *xd;
XParams *xp;
{
XFreePixmap(xd->display, pixmap);
XClearWindow(xd->display, xd->window);
}
/* ====================================================================== */
/* CopyPlane: Sizepair => (width, height) */
/* size => area = width * height */
int CopyPlane(xd, xp)
XD *xd;
XParams *xp;
{
int width, height;
int dst_x = 0, dst_y = 0;
width = xp->sizepair->s1;
height = xp->sizepair->s2;
for (i = 0; i < xp->itns; i++) {
XCopyPlane(xd->display, pixmap, xd->window, xd->gc, 0, 0,
width, height, dst_x, dst_y, 1);
if (height < DISPHEIGHT / 2)
dst_y += height;
else dst_y ++;
if ((dst_y + height) > DISPHEIGHT) {
dst_y %= DISPHEIGHT;
dst_x ++;
if ((dst_x + width ) > DISPWIDTH )
dst_x %= DISPWIDTH;
}
}
}
int i_CopyPlane(xd, xp)
XD *xd;
XParams *xp;
{
int width, height;
width = xp->sizepair->s1;
height = xp->sizepair->s2;
xp->size = width * height;
RandomFill(xd, xp);
pixmap = XCreatePixmap(xd->display, xd->window, width, height, 1);
}
int c_CopyPlane(xd, xp)
XD *xd;
XParams *xp;
{
XFreePixmap(xd->display, pixmap);
XClearWindow(xd->display, xd->window);
}
/* ====================================================================== */
/* PolyPoint: Sizepair => (0, 0) */
int PolyPoint(xd, xp)
XD *xd;
XParams *xp;
{
for (i = 0; i < xp->itns; i++)
XDrawPoints(xd->display, xd->window, xp->gc,
points, (int) xp->numobjs, CoordModeOrigin);
}
int i_PolyPoint(xd, xp)
XD *xd;
XParams *xp;
{
xp->size = 0;
xp->numobjs = POLYOBJECTS;
points = (XPoint *) malloc(xp->numobjs * sizeof(XPoint) );
for (i = 0; i < xp->numobjs; i++) {
points[i].x = rand() % DISPWIDTH;
points[i].y = rand() % DISPHEIGHT;
}
}
int c_PolyPoint(xd, xp)
XD *xd;
XParams *xp;
{
free(points);
}
/* ====================================================================== */
/* PolyLine: Sizepair => (length, 0) */
int PolyLine(xd, xp)
XD *xd;
XParams *xp;
{
for (i = 0; i < xp->itns; i++)
XDrawLines(xd->display,xd->window, xp->gc, points,
(int) xp->numobjs, CoordModeOrigin);
}
int i_PolyLine(xd, xp)
XD *xd;
XParams *xp;
{
int xoffset = XINC_LINE / 2;
int yoffset = YINC_LINE / 2;
int xdir=1, ydir=1;
xp->size = xp->sizepair->s1; /* Length of line */
xp->numobjs = POLYOBJECTS; /* Number of objects per request */
points = (XPoint *) malloc((xp->numobjs + 1 )* sizeof(XPoint));
for (i = 0; i < xp->numobjs; i+=2) {
points[i].x = xoffset ;
points[i].y = yoffset ;
points[i+1].x = xoffset + (xdir * xp->size);
points[i+1].y = yoffset + (ydir * YINC_LINE);
/* Now set xoffset and yoffset for next computation */
yoffset += ydir * YINC_LINE;
if ((yoffset >= (DISPHEIGHT - YINC_LINE) ) || (yoffset <= YINC_LINE)) {
/* y has hit an extreme. */
ydir *= -1;
xoffset += xdir * 2 * xp->size;/* Move away from previous drawing */
if ( (xoffset >= (DISPWIDTH - XINC_LINE - xp->size)) ||
(xoffset <= XINC_LINE) ) {
xdir *= -1;
xoffset+=xdir * 2 * xp->size;/*undo the last change to xoffset*/
if (xoffset <= (XINC_LINE + xp->size) )
xdir *= -1;
}
}
}
}
int c_PolyLine(xd, xp)
XD *xd;
XParams *xp;
{
free(points);
}
/* ====================================================================== */
/* PolySegment: Sizepair => (length, 0) */
int PolySegment(xd, xp)
XD *xd;
XParams *xp;
{
for (i = 0; i < xp->itns; i++)
XDrawSegments(xd->display,xd->window, xp->gc,
segments, (int) xp->numobjs);
}
int i_PolySegment(xd, xp)
XD *xd;
XParams *xp;
{
int xoffset = XINC_LINE / 2;
int yoffset = YINC_LINE / 2;
int xdir=1, ydir=1;
xp->size = xp->sizepair->s1;
xp->numobjs = POLYOBJECTS;
segments = (XSegment *) malloc((xp->numobjs + 1 )* sizeof(XSegment));
for (i = 0; i < xp->numobjs; i++) {
segments[i].x1 = xoffset ;
segments[i].y1 = yoffset ;
segments[i].x2 = xoffset + (xdir * xp->size);
segments[i].y2 = yoffset + (ydir * YINC_LINE);
/* Now set xoffset and yoffset for next computation */
yoffset += ydir * YINC_LINE;
if ((yoffset >= (DISPHEIGHT - YINC_LINE) ) || (yoffset <= YINC_LINE)) {
/* y has hit an extreme. */
ydir *= -1;
xoffset += xdir * 2 * xp->size;/* Move away from previous drawing */
if ( (xoffset >= (DISPWIDTH - XINC_LINE - xp->size)) ||
(xoffset <= XINC_LINE) ) {
xdir *= -1;
xoffset+=xdir * 2 * xp->size;/*undo the last change to xoffset*/
if (xoffset <= (XINC_LINE + xp->size) )
xdir *= -1;
}
}
}
}
int c_PolySegment(xd, xp)
XD *xd;
XParams *xp;
{
free(segments);
}
/* ====================================================================== */
/* PolyRectangle: Sizepair => (width, height) */
/* size => perimeter = 2 * (width + height) */
int PolyRectangle(xd, xp)
XD *xd;
XParams *xp;
{
for (i = 0; i < xp->itns; i++)
XDrawRectangles(xd->display, xd->window, xp->gc,
rectangles, (int) xp->numobjs);
}
int i_PolyRectangle(xd, xp)
XD *xd;
XParams *xp;
{
int xoffset = XOFFSET_RECT;
int yoffset = YOFFSET_RECT;
int xdir = 1, ydir = 1;
int width, height;
width = xp->sizepair->s1;
height = xp->sizepair->s2;
xp->size = 2 * (width + height);
xp->numobjs = POLYOBJECTS;
rectangles = (XRectangle *) malloc (xp->numobjs * sizeof(XRectangle));
for (i = 0; i < xp->numobjs; i++) {
rectangles[i].x = xoffset;
rectangles[i].y = yoffset;
rectangles[i].width = width;
rectangles[i].height = height;
yoffset += ydir * YINC_RECT;
xoffset += xdir * XINC_RECT;
if ( (xoffset >= (DISPHEIGHT - width)) || (xoffset <= XINC_RECT) ) {
xdir *= -1;
xoffset += xdir * (XINC_RECT + 1);
}
if ( (yoffset >= (DISPHEIGHT - height)) || (yoffset <= YINC_RECT) ) {
ydir *= -1;
yoffset += ydir * (YINC_RECT + 1);
}
}
}
int c_PolyRectangle(xd, xp)
XD *xd;
XParams *xp;
{
free(rectangles);
}
/* ====================================================================== */
/* PolyArc: Sizepair => (width, height) */
/* size => perimeter = 2 * (width + height) */
/* Fractional arcs are scaled from the data collected here */
int PolyArc(xd, xp)
XD *xd;
XParams *xp;
{
for (i = 0; i < xp->itns; i++)
XDrawArcs(xd->display, xd->window, xp->gc,
arcs, (int) xp->numobjs);
}
int i_PolyArc(xd, xp)
XD *xd;
XParams *xp;
{
int xoffset = XOFFSET_RECT;
int yoffset = YOFFSET_RECT;
int xdir = 1, ydir = 1;
int width, height;
width = xp->sizepair->s1;
height = xp->sizepair->s2;
xp->size = 2 * (width + height);
xp->numobjs = POLYOBJECTS;
arcs = (XArc *) malloc (xp->numobjs * sizeof(XArc));
for (i = 0; i < xp->numobjs; i++) {
arcs[i].x = xoffset;
arcs[i].y = yoffset;
arcs[i].width = width;
arcs[i].height = height;
arcs[i].angle1 = 0;
arcs[i].angle2 = 64 * 360; /* Full circle */
yoffset += ydir * YINC_RECT;
xoffset += xdir * XINC_RECT;
if ( (xoffset >= (DISPHEIGHT - width)) || (xoffset <= XINC_RECT) ) {
xdir *= -1;
xoffset += xdir * (XINC_RECT + 1);
}
if ( (yoffset >= (DISPHEIGHT - height)) || (yoffset <= YINC_RECT) ) {
ydir *= -1;
yoffset += ydir * (YINC_RECT + 1);
}
}
}
int c_PolyArc(xd, xp)
XD *xd;
XParams *xp;
{
free(arcs);
}
/* ====================================================================== */
int FillPoly(xd, xp)
XD *xd;
XParams *xp;
{
}
int i_FillPoly(xd, xp)
XD *xd;
XParams *xp;
{
}
int c_FillPoly(xd, xp)
XD *xd;
XParams *xp;
{
}
/* ====================================================================== */
/* PolyFillRectangle: Sizepair => (width, height) */
/* size => area = width * height */
int PolyFillRectangle(xd, xp)
XD *xd;
XParams *xp;
{
for (i = 0; i < xp->itns; i++)
XFillRectangles(xd->display, xd->window, xp->gc,
rectangles, (int) xp->numobjs);
}
int i_PolyFillRectangle(xd, xp)
XD *xd;
XParams *xp;
{
i_PolyRectangle(xd, xp);
xp->size = xp->sizepair->s1 * xp->sizepair->s2;
}
int c_PolyFillRectangle(xd, xp)
XD *xd;
XParams *xp;
{
c_PolyRectangle(xd, xp);
}
/* ====================================================================== */
/* PolyFillArc: Sizepair => (width, height) */
/* size => area = width * height */
/* Fractional arcs are scaled from the data collected here */
int PolyFillArc(xd, xp)
XD *xd;
XParams *xp;
{
for (i = 0; i < xp->itns; i++)
XFillArcs(xd->display, xd->window, xp->gc, arcs, (int) xp->numobjs);
}
int i_PolyFillArc(xd, xp)
XD *xd;
XParams *xp;
{
i_PolyArc(xd, xp);
xp->size = xp->sizepair->s1 * xp->sizepair->s2;
}
int c_PolyFillArc(xd, xp)
XD *xd;
XParams *xp;
{
c_PolyArc(xd, xp);
}
/* ====================================================================== */
/* PutImage: Sizepair => (width, height) */
/* size => area = width * height */
int PutImage(xd, xp)
XD *xd;
XParams *xp;
{
int width, height;
int dst_x = 0, dst_y = 0;
width = xp->sizepair->s1;
height = xp->sizepair->s2;
for (i = 0; i < xp->itns; i++) {
XPutImage(xd->display, xd->window, xp->gc, image,
0, 0, dst_x, dst_y, width, height);
dst_x ++; dst_y ++;
if ((dst_x + width ) > DISPWIDTH ) dst_x = 0;
if ((dst_y + height) > DISPHEIGHT) dst_y = 0;
}
}
int i_PutImage(xd, xp)
XD *xd;
XParams *xp;
{
xp->size = xp->sizepair->s1 * xp->sizepair->s2;
RandomFill(xd, xp);
/* Create an image */
image = XGetImage(xd->display, xd->window, 0, 0,
DISPWIDTH, DISPHEIGHT, ~0, ZPixmap);
}
int c_PutImage(xd, xp)
XD *xd;
XParams *xp;
{
XDestroyImage(image);
XClearWindow(xd->display, xd->window);
}
/* ====================================================================== */
/* GetImage: Sizepair => (width, height) */
/* size => area = width * height */
int GetImage(xd, xp)
XD *xd;
XParams *xp;
{
int width, height;
int dst_x = 0, dst_y = 0;
width = xp->sizepair->s1;
height = xp->sizepair->s2;
for (i = 0; i < xp->itns; i++) {
XDestroyImage(image);
image =XGetImage(xd->display, xd->window,
0, 0, width, height, ~0, ZPixmap);
dst_x ++; dst_y ++;
if ((dst_x + width ) > DISPWIDTH ) dst_x = 0;
if ((dst_y + height) > DISPHEIGHT) dst_y = 0;
}
}
int i_GetImage(xd, xp)
XD *xd;
XParams *xp;
{
i_PutImage(xd, xp);
}
int c_GetImage(xd, xp)
XD *xd;
XParams *xp;
{
XClearWindow(xd->display, xd->window);
}
/* ====================================================================== */
/* The following is a convenience function for filling the window with junk */
#define NUMPOINTS 100
RandomFill (xd, xp)
XD *xd;
XParams *xp;
{
points = (XPoint *) malloc (NUMPOINTS * sizeof(XPoint));
for (i = 0; i < NUMPOINTS; i ++) {
points[i].x = rand() % DISPWIDTH;
points[i].y = rand() % DISPHEIGHT;
}
XDrawPoints(xd->display, xd->window, xp->gc,
points, NUMPOINTS, CoordModeOrigin);
XDrawLines(xd->display, xd->window, xp->gc,
points, NUMPOINTS, CoordModeOrigin);
free(points);
}